package com.ccl.iot.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ccl.iot.R;

/**
 * @description：
 * @author： HD
 * @time：2017/7/7
 */

public class HDCustomDialog extends Dialog{
    Context mContext;

    public HDCustomDialog (Context context){
        super(context);
        mContext = context;
    }
    public HDCustomDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
    }

    public void setContentView(@NonNull String titleName, @NonNull View view, @Nullable ViewGroup.LayoutParams params) {
        View dialogView = initView(titleName, view);
        super.setContentView(dialogView, params);
    }

    public void setContentView(@NonNull String titleName, @NonNull View view) {
        View dialogView = initView(titleName, view);
        super.setContentView(dialogView);
    }

    public void setContentView(@NonNull String titleName, int resourceId) {
        View dialogView = initView(titleName, resourceId);
        super.setContentView(dialogView);
    }

    private View initView(@NonNull String titleName, @NonNull View view){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogView = inflater.inflate(R.layout.dialog_hdcustom_layout, null);
        LinearLayout linearLayout = (LinearLayout) dialogView.findViewById(R.id.layout_dialog);

        TextView titleNameTv = (TextView) dialogView.findViewById(R.id.tv_title);
        titleNameTv.setText(titleName);
        ImageView backBtn = (ImageView) dialogView.findViewById(R.id.btn_back);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        linearLayout.addView(view);

        return dialogView;
    }

    private View initView(@NonNull String titleName, int resourceId){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogView = inflater.inflate(R.layout.dialog_hdcustom_layout, null);
        LinearLayout linearLayout = (LinearLayout) dialogView.findViewById(R.id.layout_dialog);

        TextView titleNameTv = (TextView) dialogView.findViewById(R.id.tv_title);
        titleNameTv.setText(titleName);
        ImageView backBtn = (ImageView) dialogView.findViewById(R.id.btn_back);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        View inflate = inflater.inflate(resourceId, null);
        linearLayout.addView(inflate);

        return dialogView;
    }

}
